package com.example.serviceweb.service;

import com.google.common.util.concurrent.RateLimiter;
import com.lagou.service.HelloService;
import com.lagou.service.HelloService2;
import org.apache.dubbo.common.threadpool.support.fixed.FixedThreadPool;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description: .
 *
 * @author Wengy
 * @since JDK 1.8
 * @since 1.0.0
 */
@Service
public class TestService {
    
    @Reference
    private HelloService helloService;
    
    @Reference
    private HelloService2 helloService2;
    
    private static final RateLimiter rateLimiter = RateLimiter.create(50);
    
    public void test() throws InterruptedException {
        
        helloService.sayHello("test", 0);
        helloService2.sayHello2("test", 0);
    }
    
    public void test2() throws InterruptedException {

        ExecutorService executorService = new ThreadPoolExecutor(50, 100, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1000), new ThreadFactory() {
            AtomicInteger atomicInteger = new AtomicInteger(1);
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("MyThread" + atomicInteger.getAndIncrement());
                return thread;
            }
        }, new ThreadPoolExecutor.AbortPolicy());
        
        while(true) {
            rateLimiter.acquire();
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        helloService.methodA("testA",0);
                        helloService.methodB("testB",0);
                        helloService.methodC("testC",0);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    
                }
            });
        }
        
    }
}
